<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/26
 * Time: 13:39
 */

namespace app\sports_admin\model;


use think\Config;
use think\Model;
use think\Request;
use think\db;
use app\sports_admin\model\CustomerSchool;
use traits\model\SoftDelete;

class Train extends Model
{
    use SoftDelete;
    protected $table = "train";
    protected $id    = "id";
    protected $autoWriteTimestamp = true;
    public function getContentAttr($values)
    {
        return htmlspecialchars_decode($values);
    }
    public function getCustomer()
    {
        return $this->belongsTo('Customer','customer_id','id')->field("id,name");
    }
    public function getCreateTimeAttr($values)
    {
        return date("Y-m-d",$values);
    }
    public static function addCustomerTrain()
    {
        $data = Request::instance()->only(['id','name','login','customer_id','phone','password','phone_name','email','url','end','province','address','latitude','longitude']);
        //判断账号是否被注册过
        if(self::where('phone',$data['phone'])->find()){
            return json(['msg'=>'手机号已存在', 'errorCode'=>400]);
        }
        if(self::Where('login',$data['login'])->find()){
            return json(['msg'=>'账号已存在', 'errorCode'=>400]);
        }
        $city = $data['province'];
        $data['province'] = intval($city[0]) ? intval($city[0]) : '';
        $data['city'] = intval($city[1]) ? intval($city[1]) : '';
        $data['district'] = intval($city[2])?intval($city[2]) : '';
        $key              = Config::get('schoolPwd');
        $data['password'] = authcode(trim($data['password']),'ENCODE',$key['key'],0);
        $data['end'] = strtotime($data['end']);

        CustomerSchool::where('id',$data['id'])->update(["state"=>1]);
        //去除id
        unset($data['id']);

        $schoolId = self::create($data);
        if(!$schoolId){
            return json(['msg'=>'添加失败', 'errorCode'=>400]);
        }return json(['code'=>200,'msg'=>'添加成功']);
    }

    //获取学校列表信息
    public static function trainList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::where(function($query)use($searchName){
                if($searchName){
                    $query->where('name','like',"%{$searchName}%");
                }})
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::where(function($query)use($searchName){
                if($searchName){
                    $query->where('name','like',"%{$searchName}%");
                }})
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }

    //获取培训机构信息
    public static function trainMessagePage()
    {
        $id = Request::instance()->only('id');
        return self::with("getCustomer")->where("id",$id['id'])->find();
    }
    //更新培训机构信息
    public static function trainMessageUpdate()
    {
        $data = Request::instance()->only("id,name,content,title,address,phone_name,phone,url,email,is_recommend");
        $content = htmlspecialchars($data['content']);
        $time = time();
        return self::update(['id'=>$data['id'],'update_time'=>$time,'name'=>$data['name'],'content'=>$content,'title'=>$data['title'],'address'=>$data['address'],'phone_name'=>$data['phone_name'],'phone'=>$data['phone'],'url'=>$data['url'],'email'=>$data['email'],'is_recommend'=>$data['is_recommend']]);
    }
}